package com.platform.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.platform.service.LotteryRecordService;
import com.platform.service.UserAssetService;
import com.platform.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.AccountSnapshotEnergyEntity;
import com.platform.service.AccountSnapshotEnergyService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 能量值发放记录表Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-05-28 15:18:07
 */
@RestController
@RequestMapping("accountsnapshotenergy")
public class AccountSnapshotEnergyController {
    @Autowired
    private AccountSnapshotEnergyService accountSnapshotEnergyService;
    @Autowired
    private UserAssetService userAssetService;
    @Autowired
    private LotteryRecordService lotteryRecordServicel;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("accountsnapshotenergy:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AccountSnapshotEnergyEntity> accountSnapshotEnergyList = accountSnapshotEnergyService.queryList(query);
        int total = accountSnapshotEnergyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(accountSnapshotEnergyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("accountsnapshotenergy:info")
    public R info(@PathVariable("id") Integer id) {
        AccountSnapshotEnergyEntity accountSnapshotEnergy = accountSnapshotEnergyService.queryObject(id);

        return R.ok().put("accountSnapshotEnergy", accountSnapshotEnergy);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("accountsnapshotenergy:save")
    public R save(@RequestBody AccountSnapshotEnergyEntity accountSnapshotEnergy) {
        accountSnapshotEnergyService.save(accountSnapshotEnergy);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("accountsnapshotenergy:update")
    public R update(@RequestBody AccountSnapshotEnergyEntity accountSnapshotEnergy) {
        accountSnapshotEnergyService.update(accountSnapshotEnergy);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("accountsnapshotenergy:delete")
    public R delete(@RequestBody Integer[] ids) {
        accountSnapshotEnergyService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AccountSnapshotEnergyEntity> list = accountSnapshotEnergyService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 获得今日运动挖矿能量
     * @return
     */
    @RequestMapping("/queryEnergyCoinTotal")
    public Object queryTodaySportsEnergy(){
//        BigDecimal query = accountSnapshotEnergyService.queryToDayTeam();
        BigDecimal query = accountSnapshotEnergyService.queryToDayTeamSportEnergy();
        return R.ok().put("count",query);
    }

    /**
     * 执行能量值统计
     * @return
     */
    @RequestMapping("createEnergyStatiscs")
    public Object createEnergyStatiscs(){
        try {
            //查询列表记录
            List<Map<String,Object>> queryList = accountSnapshotEnergyService.createEnergyStaticsList();
            AccountSnapshotEnergyEntity accountSnapshotEnergyEntity = new AccountSnapshotEnergyEntity();
            accountSnapshotEnergyEntity.setTotal(userAssetService.queryTotalEnergyTotal());
            accountSnapshotEnergyEntity.setTotalDetail(accountSnapshotEnergyService.queryTotalEnergyDetail());
            accountSnapshotEnergyEntity.setHoldSpc(userAssetService.queryTotalSpcCoin());
            accountSnapshotEnergyEntity.setLotterySpend(lotteryRecordServicel.queryLotterySendTotal());
            accountSnapshotEnergyEntity.setLotteryPrice(lotteryRecordServicel.queryLotteryPriceTotal());
            accountSnapshotEnergyEntity.setDate(DateUtils.format(new Date(),"yyyy-MM-dd"));
            accountSnapshotEnergyEntity.setAddTime(new Date());
            for(Map<String,Object> param  : queryList){
                System.out.println(param.get("type").equals(1)+" ------------------------ "+param.get("type"));
                if(param.get("type").equals(19)||param.get("type").equals(20)){
                    accountSnapshotEnergyEntity.setExTotal(accountSnapshotEnergyEntity.getExTotal().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(22)||param.get("type").equals(24)){
                    accountSnapshotEnergyEntity.setBuyTotal(new BigDecimal(param.get("energy").toString()));
                }else
                if(param.get("type").equals(13)||param.get("type").equals(14)||param.get("type").equals(15)||param.get("type").equals(16)||param.get("type").equals(17)||param.get("type").equals(18)){
                    accountSnapshotEnergyEntity.setExtractTotal(accountSnapshotEnergyEntity.getExtractTotal().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(21)){
                    accountSnapshotEnergyEntity.setTaskTotal(new BigDecimal(param.get("energy").toString()));
                }else
                if(param.get("type").equals(25)||param.get("type").equals(26)||param.get("type").equals(27)||param.get("type").equals(37)||param.get("type").equals(28)||param.get("type").equals(38)){
                    accountSnapshotEnergyEntity.setReleaseSpc(accountSnapshotEnergyEntity.getReleaseSpc().add(new BigDecimal(param.get("energy").toString())));
                }else
                if(param.get("type").equals(1)){
                    accountSnapshotEnergyEntity.setExchangeSpc(new BigDecimal(param.get("energy").toString()));
                }
            }
            Integer save = accountSnapshotEnergyService.save(accountSnapshotEnergyEntity);
            if(save>0){
                System.out.println("生成能量统计成功!");
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("accountStaticsTask ---> createEnergyStatics : "+e.getMessage());
        }
        return R.error();
    }
}
